package us.icitap.timsportal.util;

import java.util.ArrayList;
import java.util.List;

import us.icitap.timsportal.ejb.PortalBeanLocal;
import us.icitap.timsportal.entity.Module;
import us.icitap.timsportal.entity.PortalSession;
import us.icitap.timsportal.entity.Role;
import us.icitap.timsportal.entity.User;
import us.icitap.timsportal.ws.util.ADConnection;

public class SessionHandler {
	
	private String username = "";
	private String password = "";
	private String portalSessionId = "";
	private boolean domainUserAuthenticated = false;
	private boolean portalUserAuthenticated = false;
	private boolean dbUserAuthenticated = false;
	private boolean openPortalSession = false;
	
	private ADConnection adConnection;
	
	public boolean isOpenPortalSession() {
		return openPortalSession;
	}

	public void setOpenPortalSession(boolean openPortalSession) {
		this.openPortalSession = openPortalSession;
	}

	private User user = null;
	private List<Module> modules;
	private String errorMessage = "";
	
	private PortalBeanLocal portalBean;
	
	public SessionHandler(PortalBeanLocal portalBean, ADConnection adConnection){
		this.portalBean = portalBean;
		this.adConnection = adConnection;
		modules = new ArrayList<Module>();
	}
	
	public void reloadSessionHandler(String previousPortalSessionId, String portalSessionId , String moduleSessionId){
		PortalSession ppSession = portalBean.searchPortalSessionInDB(previousPortalSessionId, moduleSessionId);
		
		setUsername(ppSession.getUsername());
		setDomainUserAuthenticated(true);
		setUser(portalBean.getUserEntity(ppSession.getUsername()));
		setDbUserAuthenticated(true);
		
		setPortalSessionId(portalSessionId);
		
		PortalSession pSession = new PortalSession();
		pSession.setPreviousPortalSessionId(ppSession.getPortalSessionId());
		pSession.setPortalSessionId(portalSessionId);
		pSession.setPortalEntryTime(portalBean.getSysdate());
		pSession.setUsername(ppSession.getUsername());
		portalBean.savePortalSession(pSession);
	}
	
	public boolean authenticateIntoDomain(){
		try {
			boolean value = adConnection.authenticate(username, password);
			setDomainUserAuthenticated(value);
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			setDomainUserAuthenticated(false);
			return false;
		}
		return isDomainUserAuthenticated();
	}
	
	public boolean loadSimpleUser(String username){
		try {
			user = portalBean.getUserEntity(username);			
			setDbUserAuthenticated(user!=null);
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			setDbUserAuthenticated(Boolean.FALSE);
			return Boolean.FALSE;
		}		
	}
	
	public boolean saveSessionInDB(String sessionId, String username){
		PortalSession portalSession = new PortalSession();
		portalSession.setPortalSessionId(sessionId);
		portalSession.setUsername(username);
		portalSession.setPortalEntryTime(portalBean.getSysdate());
		
		try {
			portalBean.savePortalSession(portalSession);
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return Boolean.FALSE;
		}
	}
	
	public boolean checkUserRoles(){
		try {
			for(Role r:user.getRoles()){
				if(!modules.contains(r.getModule()))
					modules.add(r.getModule());
			}
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return Boolean.FALSE;
		}
	}
	
	public boolean loginUser(){
		
			
		
		return false;
	}
	
	public boolean loadTimsPortalMainPage(){
		return false;
	}
	
	public boolean loadTimsPortalLoginPage(){
		return false;
	}
	
	public void loadTimsPortal(){
	}
	
	public boolean loadTimsPortal(String param1, String param2){
		return false;
	}
	
	public boolean searchUserSessionInDB(String username, String portalSessionId , String moduleSessionId){
		try {
			setPortalUserAuthenticated(portalBean.searchPortalSessionInDB(username, portalSessionId, moduleSessionId)!=null);
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return Boolean.FALSE;
		}
	}
	
	public boolean searchUserSessionInDB(String portalSessionId , String moduleSessionId){
		try {
			PortalSession pSession = portalBean.searchPortalSessionInDB(portalSessionId, moduleSessionId);
			setPortalUserAuthenticated(pSession!=null);
			setOpenPortalSession(pSession==null?false:pSession.getModule()==null);
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return Boolean.FALSE;
		}
	}
	
	public void showErrorMessage(){
		System.out.println("Error message: "+errorMessage);
	}
	
	public boolean ridirectUserToModule(Module selectedModule){
		try {
			PortalSession portalSession = portalBean.searchPortalSessionInDB(portalSessionId);
			portalSession.setModule(selectedModule);
			portalSession.setModuleEntryTime(portalBean.getSysdate());
			portalBean.savePortalSession(portalSession);
			return Boolean.TRUE;
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return Boolean.FALSE;
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public boolean isDomainUserAuthenticated() {
		return domainUserAuthenticated;
	}

	public void setDomainUserAuthenticated(boolean domainUserAuthenticated) {
		this.domainUserAuthenticated = domainUserAuthenticated;
	}

	public boolean isPortalUserAuthenticated() {
		return portalUserAuthenticated;
	}

	public void setPortalUserAuthenticated(boolean portalUserAuthenticated) {
		this.portalUserAuthenticated = portalUserAuthenticated;
	}

	public boolean isDbUserAuthenticated() {
		return dbUserAuthenticated;
	}

	public void setDbUserAuthenticated(boolean dbUserAuthenticated) {
		this.dbUserAuthenticated = dbUserAuthenticated;
	}

	public List<Module> getModules() {
		return modules;
	}

	public String getPortalSessionId() {
		return portalSessionId;
	}

	public void setPortalSessionId(String portalSessionId) {
		this.portalSessionId = portalSessionId;
	}

}
